package com.eye.catcher.controller;

import com.eye.catcher.param.CommonResponse;
import com.eye.catcher.param.ResponseData;
import com.eye.catcher.param.ResponseUtil;
import com.eye.catcher.constants.SbCode;
import com.eye.catcher.entity.TSysDaModule;
import com.eye.catcher.rabbit.MQDto;
import com.eye.catcher.service.generate.ITSysDaModuleService;
import com.eye.catcher.service.generate.ITSysDaRoleModuleService;
import com.eye.catcher.system.module.param.ModuleDto;
import com.eye.catcher.system.module.param.ModulePage;
import com.eye.catcher.system.module.param.ModuleResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huzhiwei
 * @since 2022-02-08
 */
@Slf4j
@Controller
@RequestMapping("/eyeCatcher-system/sys-module")
public class TSysDaModuleController extends TSysDaBaseController {

    private final static String MODULE_CODE = "/sys-module";
    private final static String MODULE_NAME = "菜单模块";

    @Autowired
    private ITSysDaModuleService moduleService;
    @Autowired
    private ITSysDaRoleModuleService roleModuleService;


    @PostMapping("/toadd")
    public String toadd(){
        return "moduleadd";
    }

    @PostMapping("/toedit")
    public String toedit(){
        return "moduleedit";
    }


    /**
     * 获取module
     * @return
     */
    @PostMapping("/getModule")
    @ResponseBody
    public ResponseData getModule() {
        String methodCode = "/getModule";
        MQDto mqDto = null;
        Object resObj = null;
        try {
            String userId = getLoginUserId();
            mqDto = new MQDto(methodCode,userId,MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_QUERY,null,null,null);
            this.sendInitRabbitMqLog(mqDto);
            ModuleResponse response = moduleService.getModule(userId);
            resObj = response;
            log.info(methodCode + response.toString());
        } catch (Exception e) {
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
            resObj = commonResponse;
        } finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(resObj));
        }
        return new ResponseUtil<>().setData(resObj);
    }

    /**
     * 获取getAllModuleList
     * @return
     */
    @PostMapping("/getAllModuleList")
    @ResponseBody
    public ResponseData getAllModuleList() {
        String methodCode = "/getAllModuleList";
        MQDto mqDto = null;
        Object resObj = null;
        try {
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_QUERY,null,null,null);
            this.sendInitRabbitMqLog(mqDto);
            ModuleResponse response = moduleService.getAllModuleList();
            log.info(methodCode + response.toString());
            resObj = response;
        } catch (Exception e) {
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
            resObj = commonResponse;
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(resObj));
        }
        return new ResponseUtil<>().setData(resObj);
    }

    @PostMapping("/addModule")
    @ResponseBody
    public ResponseData addModule(@RequestBody TSysDaModule module) {
        String methodCode = "/addModule";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_ADD,module.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.moduleService.addModule(module);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    @PostMapping("/editModule")
    @ResponseBody
    public ResponseData editModule(@RequestBody TSysDaModule module) {
        String methodCode = "/editModule";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_EDIT,module.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.moduleService.editModule(module);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }
    @PostMapping("/updateStausByIds")
    @ResponseBody
    public ResponseData updateStausByIds(@RequestParam(value = "ids") String ids) {
        String methodCode = "/updateStausByIds";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_EDIT,"ids："+ids,null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.moduleService.updateStausByIds(ids);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }


    @PostMapping("/deleteModuleByIds")
    @ResponseBody
    public ResponseData deleteModuleByIds(@RequestParam(value = "moduleIds") String moduleIds) {
        String methodCode = "/deleteModuleByIds";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_DELETE,"ids: "+moduleIds,null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.moduleService.deleteModuleByIds(moduleIds);
            this.roleModuleService.deleteRoleModuleByModuleIds(moduleIds);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        } finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    @PostMapping("/getModuleList")
    @ResponseBody
    public ResponseData getModuleList(@RequestBody ModuleDto module) {
        String methodCode = "/getModuleList";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,
                    getClass(),SbCode.OPERATION_QUERY,module.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            commonResponse = CommonResponse.success();
            commonResponse.setData(
                this.moduleService.getPageModuleList(
                    ModulePage.builder()
                        .module(module)
                        .page(this.getPageInfo(module))
                    .build()
                )
            );
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }


}
